﻿using System;

namespace _5E.Utils.DbHelper.EasySQL.Creator
{
    /// <summary>
    /// 程序说明：创建插入SQL
    /// 创建作者：ahl5esoft
    /// 创建时间：2012-04-16
    /// </summary>
    class InsertCreator : BaseCreator, ICustomColumn
    {
        #region 变量

        private const string INSERT_SQL_FORMAT = "INSERT INTO {0}({1}) VALUES({2})";

        private string[] m_Columns = null;

        #endregion

        #region 实例方法

        public ICustomColumn SetColumns(string[] propertyNames)
        {
            m_Columns = propertyNames;
            return this;
        }

        public override string CreateSQL()
        {
            var columnNames = Array.ConvertAll(m_Columns, GetColumnName);
            var columnString = ToColumnString(columnNames);
            var propertyString = ToParameterString(m_Columns);
            var sql = string.Format(INSERT_SQL_FORMAT, TableName, columnString, propertyString);
            return sql;
        }

        private string ToParameterString(string[] propertyNames)
        {
            var parameters = Array.ConvertAll(propertyNames, f => string.Format(":{0}", f));
            var prameterString = ToColumnString(parameters);
            return prameterString;
        }

        protected override void Reset()
        {
            m_Columns = null;
        }

        #endregion
    }
}
